package com.oracle.lingshangejia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.PositionListAdapter;
import com.oracle.lingshangejia.bean.LocationInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 位置列表
 */
public class PositionListActivity extends AppCompatActivity /*implements View.OnClickListener*/ {
   /*   //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
  public AMapLocationClientOption mLocationOption = null;
    public double Longitude, Latitude;// 经度 纬度
    private RecyclerView position_list;
    private PositionListAdapter mAdapter;
    private ArrayList<LocationInfo> mList;
    private boolean isClick=false;
    private TextView tv_title;
    private ImageButton ib_back;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_positionlist);
        initMapLocation();
        initView();
    }

    private void initView() {
        position_list=findViewById(R.id.position_list);
        tv_title=findViewById(R.id.tv_title);
        tv_title.setText("位置信息");
        ib_back=findViewById(R.id.ib_back);
        ib_back.setVisibility(View.VISIBLE);
        ib_back.setOnClickListener(this);
        mList=new ArrayList<>();
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mAdapter=new PositionListAdapter(this,mList);
        position_list.setLayoutManager(linearLayoutManager);
        position_list.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new PositionListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int pos) {
                isClick=true;
                //数据是使用Intent返回
                Intent intent = new Intent();
                //把返回数据存入Intent
                intent.putExtra("address", mList.get(pos).getAddress());
                //设置返回数据
                PositionListActivity.this.setResult(RESULT_OK, intent);
                //关闭Activity
                PositionListActivity.this.finish();
            }
        });

    }
    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation.getErrorCode() == 0) {
                //停止定位后，本地定位服务并不会被销毁
                mLocationClient.stopLocation();
                //销毁定位客户端，同时销毁本地定位服务。
                mLocationClient.onDestroy();
                Longitude = aMapLocation.getLongitude();
                Latitude = aMapLocation.getLatitude();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(aMapLocation.getTime());
                df.format(date);//定位时间
                PoiSearch.Query query = new PoiSearch.Query("", "", "");
                query.setPageSize(20);
                PoiSearch search = new PoiSearch(PositionListActivity.this, query);
                search.setBound(new PoiSearch.SearchBound(new LatLonPoint(Latitude, Longitude), 10000));
                search.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
                    @Override
                    public void onPoiSearched(PoiResult poiResult, int i) {
                        PoiSearch.Query query = poiResult.getQuery();
                        ArrayList<PoiItem> pois = poiResult.getPois();
                                for (PoiItem poi : pois) {
                                    String name = poi.getTitle();
                                    String snippet = poi.getSnippet();
                                    LocationInfo info = new LocationInfo();
                                    info.setAddress(snippet);
                                    LatLonPoint point = poi.getLatLonPoint();
                                    info.setName(name);
                                    info.setLatitude(point.getLatitude());
                                    info.setLongtitude(point.getLongitude());
                                    mList.add(info);
                                    Log.d("haha", "poi" + snippet);

                                }
                                mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onPoiItemSearched(PoiItem poiItem, int i) {

                    }
                });
                search.searchPOIAsyn();
                //  Log.e("PositionService", aMapLocation.getAddress() + ".." + aMapLocation.getLatitude() + ".." + aMapLocation.getLongitude()+".."+df.format(date));

            } else {
                //截取第一个空格之前的错误日志
                Toast.makeText(PositionListActivity.this, aMapLocation.getErrorInfo(), Toast.LENGTH_SHORT).show();
                //停止定位后，本地定位服务并不会被销毁
                mLocationClient.stopLocation();
                //销毁定位客户端，同时销毁本地定位服务。
                mLocationClient.onDestroy();
            }

        }
    };
    *//**
     * 初始化地图定位
     *//*
    private void initMapLocation() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
        mLocationOption.setInterval(1000);
        //启动定位
        mLocationClient.startLocation();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if(!isClick){
            //数据是使用Intent返回
            Intent intent = new Intent();
            if(mList.size()!=0){
                //把返回数据存入Intent
                intent.putExtra("address", mList.get(0).getAddress());
            }
            //设置返回数据
            PositionListActivity.this.setResult(RESULT_OK, intent);
            //关闭Activity
            PositionListActivity.this.finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (event.getKeyCode()){
            case KeyEvent.KEYCODE_BACK:
                if(mList.size()!=0){
                    //数据是使用Intent返回
                    Intent intent = new Intent();
                    //把返回数据存入Intent
                    intent.putExtra("address", mList.get(0).getAddress());
                    //设置返回数据
                    PositionListActivity.this.setResult(RESULT_OK, intent);
                    //关闭Activity
                    PositionListActivity.this.finish();
                }
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_back:
                if(mList.size()!=0){
                    //数据是使用Intent返回
                    Intent intent = new Intent();
                    //把返回数据存入Intent
                    intent.putExtra("address", mList.get(0).getAddress());
                    //设置返回数据
                    PositionListActivity.this.setResult(RESULT_OK, intent);
                    //关闭Activity
                    PositionListActivity.this.finish();
                }
                break;
        }
    }*/
}
